package linear_list.singlelinkedlist;

public class SingleLinkedList {
    private int size;
    private Node head;

    public String toString() {
        String ret = "";
        Node pHead = head;
        while (pHead != null) {
            ret += pHead.data;
            ret += "->";
            pHead = pHead.next;
        }
        ret += "NULL";
        return ret;
    }
    //头插
    public void addFirst(int data) {
        addIndex(0,data);
    }

    //尾插法
    public void addLast(int data){
        addIndex(size,data);
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size){
            System.err.println("input error");
            return false;
        }
        Node newNode = new Node(data);
        Node pHead = head;
        while(index > 1){
            pHead = pHead.next;
            index--;
        }
        if(pHead == null){
            head = newNode;
        }else if(index == 0) {
            newNode.next = head;
            head = newNode;
        } else {
            newNode.next = pHead.next;
            pHead.next = newNode;
        }
        size++;

        return true;
    }
        //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node pHead = head;
        while(pHead != null){
            if(pHead.data == key){
                return  true;
            }
            pHead = pHead.next;
        }
        return false;
    }
        //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node pHead = head;
        if(head.data == key){
            head = head.next;
            size--;
        } else{
            while (pHead.next != null) {
                if(pHead.next.data == key){
                    Node temp = pHead.next;
                    pHead.next = pHead.next.next;
                    temp.next = null;
                    size--;
                    break;
                }
                pHead = pHead.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        //删除头节点以及后面有连续等于key值
        while(head != null && head.data == key){
            head = head.next;
            size--;
        }
        //链表全部都等于key,就直接返回
        if(head != null){
            Node pHead = head;
            while (pHead.next != null) {
                //删除节点时,后面的节点会接上,所以有删除操作就不要往后移
                if (pHead.next.data == key) {
                    Node temp = pHead.next;
                    pHead.next = pHead.next.next;
                    temp.next = null;
                    size--;
                } else {
                    pHead = pHead.next;
                }
            }
        }
    }
        //得到单链表的长度
    public int size(){
        return size;
    }

    public void clear(){
        clearHelper(head);
        head = null;
    }

    private void clearHelper(Node head) {
        if(head == null){
            return;
        }
        clearHelper(head.next);
        head.next = null;
    }

}
class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
    }
}